package com.matrix.product.config;

import com.dtp.core.spring.EnableDynamicTp;
import com.dtp.core.support.DynamicTp;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * <p>
 * 自定义线程池配置
 * </p>
 *
 * @author 有点甜
 * @since 2025/8/4
 */
@Configuration
@EnableAsync
@EnableDynamicTp
public class ThreadPoolConfig {

    public static final String CACHE_REBUILD_EXECUTOR = "cacheRebuildExecutor";

    @DynamicTp("cacheRebuildExecutor")
    @Bean(name = CACHE_REBUILD_EXECUTOR)
    public Executor cacheRebuildExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(4);
        executor.setMaxPoolSize(8);
        executor.setQueueCapacity(200);
        executor.setThreadNamePrefix("cache-rebuild-");
        executor.setRejectedExecutionHandler(new java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}
